class Admin::LogisticsCompaniesController < AdminController
  def index
    @logistics_companies = LogisticsCompany.order("created_at ASC").page(params[:page]).per(params[:per])
  end

  def new
    @logistics_company = LogisticsCompany.new
  end

  def create
    @logistics_company = LogisticsCompany.new advertisement_params
    if @logistics_company.save
      redirect_to admin_logistics_companies_path, notice: "保存成功！"
    else
      redirect_to admin_logistics_companies_path, notice: "保存失败！"
    end
  end

  def edit
    @logistics_company = LogisticsCompany.find params[:id]
  end

  def update
    @logistics_company = LogisticsCompany.find params[:id]
    if @logistics_company.update advertisement_params
      redirect_to admin_logistics_companies_path, notice: "更新成功！"
    else
      redirect_to admin_logistics_companies_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @logistics_company = LogisticsCompany.find params[:id]
    if @logistics_company.destroy
      redirect_to admin_logistics_companies_path, notice: "删除成功."
    else
      redirect_to admin_logistics_companies_path, notice: "删除失败."
    end
  end

  private

  def advertisement_params
    params.require(:logistics_company).permit(:name, :api_key, :image)
  end
end
